﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class TaskNotification : System.Web.UI.UserControl
    {
        public bool edit = false;
        public int taskid = 0;
        public Task task;
        public string taskTitle = "New Task";
        public string createdBy = string.Empty;
        public string createDate = string.Empty;
        public string assignDate = string.Empty;
        public string assignedTo = string.Empty;
        public string taskLocation = string.Empty;
        public string taskNotes;
        public int taskTypeId = 0;
        public int taskPriorityId = 0;
        public string[] taskPriorityOptions;
        public string[] taskTypeOptions;
        public string taskComplete = string.Empty;
        public int taskContainerId = 0;
        public string[] complexityOptions;
        public string[] memberOptions;
        public decimal taskHours = 0;


        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            complexityOptions = BuildTaskComplexityOptions(TaskComplexity.FindAll());
            memberOptions = BuildMemberList();

            using (new SessionScope())
            {
                taskLocation = Common.GetRecursiveTaskTreePath(TaskContainer.Find(taskContainerId));
                task = Task.Find(taskid) ?? new Task();
                if (task.ID > 0)
                {

                    taskTitle = task.Title;
                    createDate = task.CreateDateTime.Value.ToShortDateString();
                    if (task.AssignedDateTime.HasValue)
                    {
                        assignDate = task.AssignedDateTime.Value.ToShortDateString();
                    }
                    if (task.ClosedDateTime.ToString() != "1/1/1900 12:00:00 AM")
                    {
                        edit = false;
                        taskComplete = "1";
                    }
                    else
                    {
                        taskComplete = "0";
                    }
                    taskTypeId = task.TaskType.ID;
                    taskPriorityId = task.Priority.ID;
                    taskTypeOptions = BuildTaskTypeOptions(TaskType.FindAll());
                    taskPriorityOptions = BuildTaskPriorityOptions(Priority.FindAll());
                    taskNotes = TaskView.GetTaskNotes(task.ID, true);
                    TaskNote[] tna = TaskNote.FindAll(Expression.Eq("Task", task));
                    foreach (TaskNote tn in tna) {
                        taskHours += tn.Hours;
                    }
                    TaskContainerItem ti = new TaskContainerItem();
                    TaskContainerItem[] containerItems = TaskContainerItem.FindAllByProperty("TaskID", task.ID);
                    if (containerItems.Length > 0)
                    {

                        if (containerItems[0].TaskContainer.ParentContainerID > 0)
                        {
                            taskLocation = Common.GetRecursiveTaskTreePath(TaskContainer.Find(containerItems[0].TaskContainer.ParentContainerID)) + " : " + containerItems[0].TaskContainer.Label;
                        }
                        else
                        {
                            taskLocation = containerItems[0].TaskContainer.Label;
                        }
                    }
                    Member created = Member.Find(task.CreateMemberID);
                    createdBy = created.FirstName + " " + created.LastName;
                    if (task.AssignedMemberID > 0)
                    {
                        Member assigned = Member.Find(task.AssignedMemberID);
                        assignedTo = assigned.FirstName + " " + assigned.LastName;
                    }
                }
                else
                {
                    taskTypeId = 0;
                    task.ClosedDateTime = new DateTime(1900, 1, 1);
                    taskPriorityId = 0;
                    taskTypeOptions = BuildTaskTypeOptions(TaskType.FindAll());
                    taskPriorityOptions = BuildTaskPriorityOptions(Priority.FindAll());
                    taskNotes = "";
                    createdBy = Session["FullName"].ToString();
                    createDate = DateTime.Now.ToShortDateString();
                }
            }

            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        private string[] BuildMemberList()
        {
            Member[] members = Member.FindAll(Order.Asc("LastName"));
            string[] memberList = new string[members.Length + 1];
            int i = 1;
            memberList[0] = "0=";
            foreach (Member member in members)
            {
                memberList[i] = member.ID + "=" + member.FirstName + " " + member.LastName;
                i++;
            }
            return memberList;
        }

        private string[] BuildTaskPriorityOptions(Priority[] priorities)
        {
            string[] priorityList = new string[priorities.Length];
            using (new SessionScope())
            {
                int i = 0;
                foreach (Priority priority in priorities)
                {
                    priorityList[i] = priority.ID + "=" + priority.Label;
                    i++;
                }
            }
            return priorityList;
        }
        private string[] BuildTaskComplexityOptions(TaskComplexity[] complexities)
        {
            string[] complexityList = new string[complexities.Length];
            using (new SessionScope())
            {
                int i = 0;
                foreach (TaskComplexity complexity in complexities)
                {
                    complexityList[i] = complexity.ID + "=" + complexity.Label;
                    i++;
                }
            }
            return complexityList;
        }

        private string[] BuildTaskTypeOptions(TaskType[] taskTypes)
        {
            string[] typeList = new string[taskTypes.Length];
            int i = 0;
            foreach (TaskType type in taskTypes)
            {
                typeList[i] = type.ID +"="+ type.Label;
                i++;
            }
            return typeList;
        }

        

        
    }
}